package com.ysxsoft.wdg.ui.activity.center_zy;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.center_zy.ZyFlLAdapter;
import com.ysxsoft.wdg.adapter.center_zy.ZyFlRAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.zy.RuleBean;
import com.ysxsoft.wdg.net.CallbackCode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ClassifyActivity extends BaseActivity {

    public static void start(Context context){
        Intent intent = new Intent(context,ClassifyActivity.class);
        context.startActivity(intent);
    }


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private ZyFlLAdapter lAdapter;
    private ZyFlRAdapter rAdapter;
    private List<RuleBean> list = new ArrayList<>();
    private String categoryId="";
    @Override
    protected int getLayoutId() {
        return R.layout.activity_classify;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("全部分类");

        categoryId=getIntent().getStringExtra("categoryId");

        lAdapter = new ZyFlLAdapter(R.layout.item_fenli_l);
        rAdapter = new ZyFlRAdapter(R.layout.item_zy_fl_r);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        recyclerview.setAdapter(lAdapter);
        recyclerview2.setAdapter(rAdapter);



        multStatusView.showLoading();
        mApiHelper.getZyFenlei(new Observer<BaseBean<List<RuleBean>>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<List<RuleBean>> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    list = baseBean.getData();
                    lAdapter.setNewData(list);

                    if(categoryId==null){
                        if (list.size() > 0){
                            setSelect(0);
                        }
                    }else{//首页广告点击选中分类
                        int p=0;
                        for (int i = 0; i < list.size(); i++) {
                            RuleBean r=list.get(i);
                            if(categoryId.equals(r.getId())){
                                p=i;
                                break;
                            }
                        }
                        setSelect(p);
                    }
                }else {
                    multStatusView.showEmpty();
                }
            }

            @Override
            public void onError(Throwable e) {
                multStatusView.showError();
            }

            @Override
            public void onComplete() {
                multStatusView.showContent();
            }
        });
    }

    private void setSelect(int tPosition) {
        lAdapter.setSelect(tPosition);
        rAdapter.setNewData(list.get(tPosition).getSub());
    }

    @Override
    protected void setListener() {
        lAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setSelect(position);
            }
        });

        rAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RuleBean.SubBean item = rAdapter.getItem(position);
                ZyCateListActivity.start(mContext,item.getTitle(),item.getId());
            }
        });
    }


    @OnClick(R.id.title_finish)
    public void onViewClicked() {
        finish();
    }
}
